{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Shortest Distance to Target String in a Circular Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: closetTarget"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #到目标字符串的最短距离"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong> 开始的 <strong>环形</strong> 字符串数组 <code>words</code> 和一个字符串 <code>target</code> 。<strong>环形数组</strong> 意味着数组首尾相连。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>形式上， <code>words[i]</code> 的下一个元素是 <code>words[(i + 1) % n]</code> ，而 <code>words[i]</code> 的前一个元素是 <code>words[(i - 1 + n) % n]</code> ，其中 <code>n</code> 是 <code>words</code> 的长度。</li>\n",
    "</ul>\n",
    "\n",
    "<p>从 <code>startIndex</code> 开始，你一次可以用 <code>1</code> 步移动到下一个或者前一个单词。</p>\n",
    "\n",
    "<p>返回到达目标字符串 <code>target</code> 所需的最短距离。如果 <code>words</code> 中不存在字符串 <code>target</code> ，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"hello\",\"i\",\"am\",\"leetcode\",\"hello\"], target = \"hello\", startIndex = 1\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>从下标 1 开始，可以经由以下步骤到达 \"hello\" ：\n",
    "- 向右移动 3 个单位，到达下标 4 。\n",
    "- 向左移动 2 个单位，到达下标 4 。\n",
    "- 向右移动 4 个单位，到达下标 0 。\n",
    "- 向左移动 1 个单位，到达下标 0 。\n",
    "到达 \"hello\" 的最短距离是 1 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"a\",\"b\",\"leetcode\"], target = \"leetcode\", startIndex = 0\n",
    "<strong>输出：</strong>1\n",
    "<strong>解释：</strong>从下标 0 开始，可以经由以下步骤到达 \"leetcode\" ：\n",
    "- 向右移动 2 个单位，到达下标 3 。\n",
    "- 向左移动 1 个单位，到达下标 3 。\n",
    "到达 \"leetcode\" 的最短距离是 1 。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>words = [\"i\",\"eat\",\"leetcode\"], target = \"ate\", startIndex = 0\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>因为 words 中不存在字符串 \"ate\" ，所以返回 -1 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= words.length &lt;= 100</code></li>\n",
    "\t<li><code>1 &lt;= words[i].length &lt;= 100</code></li>\n",
    "\t<li><code>words[i]</code> 和 <code>target</code> 仅由小写英文字母组成</li>\n",
    "\t<li><code>0 &lt;= startIndex &lt; words.length</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [shortest-distance-to-target-string-in-a-circular-array](https://leetcode.cn/problems/shortest-distance-to-target-string-in-a-circular-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [shortest-distance-to-target-string-in-a-circular-array](https://leetcode.cn/problems/shortest-distance-to-target-string-in-a-circular-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[\"hello\",\"i\",\"am\",\"leetcode\",\"hello\"]\\n\"hello\"\\n1', '[\"a\",\"b\",\"leetcode\"]\\n\"leetcode\"\\n0', '[\"i\",\"eat\",\"leetcode\"]\\n\"ate\"\\n0']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for k,v in enumerate(words):\n",
    "            if v == target:\n",
    "                ans = min(ans,abs(k-startIndex),n-abs(k-startIndex))\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n = len(words)\n",
    "        # 环形数组 意味着数组首尾相连\n",
    "        # words[i] 的下一个元素是 words[(i + 1) % n]\n",
    "        # 而 words[i] 的前一个元素是 words[(i - 1 + n) % n]\n",
    "        min_l = len(words)\n",
    "        temp_index = startIndex\n",
    "        temp_after = 0\n",
    "        # 先向后遍历\n",
    "        while words[temp_index] != target:\n",
    "            temp_after += 1\n",
    "            temp_index = (temp_index + 1) % n\n",
    "        min_l = min(min_l, temp_after)\n",
    "        temp_index = startIndex\n",
    "        temp_pre = 0\n",
    "        # 再向前遍历\n",
    "        while words[temp_index] != target:\n",
    "            temp_pre += 1\n",
    "            temp_index = (temp_index - 1 + n) % n\n",
    "        min_l = min(min_l, temp_pre)\n",
    "        return min_l\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        for i in range(n):\n",
    "            if words[(startIndex+i)%n]==target or words[(startIndex-i+n)%n]==target:\n",
    "                return i\n",
    "        return -1        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        res = len(words)\n",
    "        for i in range(len(words)):\n",
    "            if words[i] == target:\n",
    "                res = min(res,min(abs(i-startIndex),len(words)-abs(i-startIndex)))\n",
    "        return -1 if res==len(words) else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n=len(words)\n",
    "        # words=words+words\n",
    "        # startIndex+=n\n",
    "        # i,j=startIndex,startIndex\n",
    "        # while i>=0 and words[i]!=target: \n",
    "        #     i-=1\n",
    "        # ans=float(\"inf\")\n",
    "        # if i>=0 and words[i]==target:\n",
    "        #     ans=startIndex-i \n",
    "        # while j<2*n and words[j]!=target:\n",
    "        #     j+=1\n",
    "        # if j<2*n and words[j]==target:\n",
    "        #     ans=min(ans, j-startIndex)\n",
    "        # return ans if ans<float(\"inf\") else -1\n",
    "\n",
    "# class Solution:\n",
    "#     def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "#         n=len(words)\n",
    "#         # words=words+words\n",
    "#         # startIndex+=n\n",
    "#         i,j=startIndex,startIndex\n",
    "#         while i>=0 and words[i]!=target: \n",
    "#             i-=1\n",
    "#         ans=float(\"inf\")\n",
    "#         if i>=0 and words[i]==target:\n",
    "#             ans=min(startIndex-i,n-startIndex+i) \n",
    "#         ### 检测到startIndex+n即可\n",
    "#         while j<startIndex+n and words[j%n]!=target:\n",
    "#             j+=1\n",
    "#         if j<startIndex+n and words[j%n]==target:\n",
    "#             ans=min(ans, j-startIndex,n-j+startIndex)\n",
    "#         return ans if ans<float(\"inf\") else -1\n",
    "\n",
    "# ### 遍历\n",
    "# class Solution:\n",
    "#     def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "#         lst=[]\n",
    "#         n=len(words)\n",
    "#         for i,w in enumerate(words):\n",
    "#             if w==target:\n",
    "#                 lst.append(i)\n",
    "#         # n=len(w)\n",
    "#         if not lst:return -1\n",
    "#         ans=float(\"inf\")\n",
    "#         for idx in lst:\n",
    "#             ans=min(ans,abs(idx-startIndex),n-abs(idx-startIndex))\n",
    "#         return ans\n",
    "\n",
    "### 灵茶山\n",
    "class Solution:\n",
    "\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "\n",
    "        ans = n = len(words)\n",
    "\n",
    "        for i, w in enumerate(words):\n",
    "\n",
    "            if w == target:\n",
    "\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "\n",
    "        return ans if ans < n else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        res = len(words)\n",
    "        for i in range(len(words)):\n",
    "            if words[i] == target:\n",
    "                res = min(res,min(abs(i-startIndex),len(words)-abs(i-startIndex)))\n",
    "        return -1 if res==len(words) else res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        # 双指针\n",
    "        n = len(words)\n",
    "        for i in range(n // 2 + 1):\n",
    "            if words[(startIndex + i) % n] == target:\n",
    "                return i\n",
    "            if words[(startIndex - i) % n] == target:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        i, j = (startIndex - 1 + n) % n,  (startIndex + 1 + n) % n\n",
    "        ans = 0\n",
    "        if words[startIndex] == target:\n",
    "            return 0\n",
    "        while i != j:\n",
    "            if words[i] == target or words[j] == target:\n",
    "                return ans + 1\n",
    "            else:\n",
    "                ans += 1\n",
    "                i, j = (i - 1 + n) % n,  (j + 1 + n) % n\n",
    "        if words[i] == target:\n",
    "            return ans + 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        else:\n",
    "            ans = len(words)\n",
    "            for i in range(len(words)):\n",
    "                if words[i] == target:\n",
    "                    ans = min(abs(i-startIndex), ans, abs(len(words)-abs(i-startIndex)))\n",
    "            return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        ans = set()\n",
    "        for i in range(n):\n",
    "            if words[i] == target:\n",
    "                ans.add(i)\n",
    "        if not ans:\n",
    "            return -1\n",
    "        print(ans)\n",
    "        res = n\n",
    "        for num in ans:\n",
    "            res = min(res, abs(n-num+startIndex), abs(num-startIndex), abs(n-startIndex+num))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        ans=len(words)\n",
    "        for i,x in enumerate(words):\n",
    "            if x==target:\n",
    "                ans=min(ans,abs(i-startIndex),len(words)+i-startIndex,len(words)-i+startIndex)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        if not target in words:\n",
    "            return -1\n",
    "        ans = n\n",
    "        i = startIndex\n",
    "        while words[(i) % n] != target:\n",
    "            i += 1\n",
    "        ans = min(ans, abs(i - startIndex))\n",
    "        i = startIndex\n",
    "        while words[(i) % n] != target:\n",
    "            i -= 1\n",
    "        ans = min(ans, abs(i - startIndex))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        byWord = defaultdict(list)\n",
    "        for i, s in enumerate(words):\n",
    "            byWord[s].append(i)\n",
    "        indice = byWord.get(target, None)\n",
    "        if indice is None: return -1\n",
    "        off = bisect_right(indice, startIndex)\n",
    "        candidates = []\n",
    "        if off:\n",
    "            candidates.append(startIndex - indice[off-1])\n",
    "        else:\n",
    "            candidates.append(startIndex + len(words) - indice[-1])\n",
    "        if off < len(indice):\n",
    "            candidates.append(indice[off] - startIndex)\n",
    "        else:\n",
    "            candidates.append(indice[0] + len(words) - startIndex)\n",
    "        return min(candidates)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        res = float('inf')\n",
    "        for i, word in enumerate(words):\n",
    "            if word != target: continue\n",
    "\n",
    "            res = min(res, min(abs(i-startIndex), len(words) - abs(i-startIndex)))\n",
    "        \n",
    "        return -1 if res==float('inf') else res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n = len(words)\n",
    "        words = words*2\n",
    "        res = inf\n",
    "        # 正向找\n",
    "\n",
    "        for i in range(startIndex, 2*n):\n",
    "            if words[i] == target:\n",
    "                res = min(res, i - startIndex)\n",
    "                break\n",
    "        # 逆向找\n",
    "        for k in range(startIndex+n, -1, -1):\n",
    "            if words[k] == target:\n",
    "                res = min(res, startIndex+ n - k)\n",
    "                break\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n=len(words)\n",
    "        #[a,b,leetcode,a,b,leetcode,a,b,leetcode]\n",
    "        words.extend(words)\n",
    "        words.extend(words)\n",
    "        startIndex += n\n",
    "        ans=-1\n",
    "        for i in range(startIndex,3*n):\n",
    "            if words[i]==target:\n",
    "                ans=i-startIndex\n",
    "                break\n",
    "        for i in range(startIndex,-1,-1):\n",
    "            if words[i]==target:\n",
    "                ans=min(ans,startIndex-i)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        res = -1\n",
    "        for i, word in enumerate(words):\n",
    "            if word == target:\n",
    "                if res == -1:\n",
    "                    res = min(abs(startIndex - i), n-abs(startIndex - i))\n",
    "                else:\n",
    "                    res = min(abs(startIndex - i), n-abs(startIndex - i), res)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n=len(words)\n",
    "        # words=words+words\n",
    "        # startIndex+=n\n",
    "        # i,j=startIndex,startIndex\n",
    "        # while i>=0 and words[i]!=target: \n",
    "        #     i-=1\n",
    "        # ans=float(\"inf\")\n",
    "        # if i>=0 and words[i]==target:\n",
    "        #     ans=startIndex-i \n",
    "        # while j<2*n and words[j]!=target:\n",
    "        #     j+=1\n",
    "        # if j<2*n and words[j]==target:\n",
    "        #     ans=min(ans, j-startIndex)\n",
    "        # return ans if ans<float(\"inf\") else -1\n",
    "\n",
    "# class Solution:\n",
    "#     def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "#         n=len(words)\n",
    "#         # words=words+words\n",
    "#         # startIndex+=n\n",
    "#         i,j=startIndex,startIndex\n",
    "#         while i>=0 and words[i]!=target: \n",
    "#             i-=1\n",
    "#         ans=float(\"inf\")\n",
    "#         if i>=0 and words[i]==target:\n",
    "#             ans=min(startIndex-i,n-startIndex+i) \n",
    "#         ### 检测到startIndex+n即可\n",
    "#         while j<startIndex+n and words[j%n]!=target:\n",
    "#             j+=1\n",
    "#         if j<startIndex+n and words[j%n]==target:\n",
    "#             ans=min(ans, j-startIndex,n-j+startIndex)\n",
    "#         return ans if ans<float(\"inf\") else -1\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        lst=[]\n",
    "        n=len(words)\n",
    "        for i,w in enumerate(words):\n",
    "            if w==target:\n",
    "                lst.append(i)\n",
    "        # n=len(w)\n",
    "        if not lst:return -1\n",
    "        ans=float(\"inf\")\n",
    "        for idx in lst:\n",
    "            ans=min(ans,abs(idx-startIndex),n-abs(idx-startIndex))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n=len(words)\n",
    "        pos=[]\n",
    "        for i in range(n):\n",
    "            if words[i]==target:\n",
    "                tmp=abs(startIndex-i)\n",
    "                pos.append(min(tmp,n-tmp))\n",
    "        return min(pos)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        i = 0\n",
    "        ans = inf\n",
    "        while i < n:\n",
    "            if words[(i + startIndex) % n] == target:\n",
    "                return i\n",
    "            if words[(startIndex - i + n) % n] == target:\n",
    "                return i\n",
    "            i += 1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i,w in enumerate(words):\n",
    "            if w==target:\n",
    "                ans = min(ans,abs(i-startIndex),n-abs(i-startIndex))\n",
    "        return ans if ans<n else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        length = words.__len__()\n",
    "        if words[startIndex] == target:\n",
    "            return 0\n",
    "        index = (startIndex + 1) % length\n",
    "        pos = []\n",
    "        while index != startIndex:\n",
    "            if words[index] == target:\n",
    "                pos.append(abs(startIndex-index))\n",
    "            index = (index + 1) % length\n",
    "        \n",
    "\n",
    "        index = (length + startIndex - 1) % length\n",
    "        while index != startIndex:\n",
    "            if words[index] == target:\n",
    "                pos.append(length-abs(index-startIndex))\n",
    "            index = (index - 1) % length\n",
    "        # print(pos)\n",
    "        return min(pos) if pos.__len__() > 0 else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if words[startIndex] == target: return 0\n",
    "        n  = len(words)\n",
    "        ans = 1\n",
    "        f = (startIndex + 1) % n\n",
    "        e = (startIndex - 1 + n) % n\n",
    "        while e != f:\n",
    "            if words[f] == target or words[e] == target: return ans\n",
    "            f = (f + 1) % n\n",
    "            e = (e - 1 + n) % n\n",
    "            ans += 1\n",
    "        if words[e] == target:\n",
    "            return ans\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        i,n=0,len(words)\n",
    "        while i<n:\n",
    "            if words[(startIndex+i)%n]==target:\n",
    "                return i\n",
    "            if words[(startIndex-i+n)%n]==target:\n",
    "                return i\n",
    "            i+=1\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n=res=len(words)\n",
    "\n",
    "        for i,j in enumerate(words):\n",
    "            if j==target:\n",
    "                res=min(res,abs(i-startIndex),n-abs(i-startIndex))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words: return -1\n",
    "        l,r=startIndex,startIndex\n",
    "        n=len(words)\n",
    "        while words[(l+n)%n]!=target and words[r%n]!=target:\n",
    "            l-=1\n",
    "            r+=1\n",
    "        return startIndex-l if words[(l-1+n)%n]==target else r-startIndex"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n",
    "\n",
    "\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if not target in words:\n",
    "            return -1\n",
    "        \n",
    "        r = 0\n",
    "        while words[(r+startIndex)%len(words)]!=target:\n",
    "            r+=1\n",
    "        \n",
    "        l = 0\n",
    "        while words[(-l+startIndex)%len(words)]!=target:\n",
    "            l+=1\n",
    "        \n",
    "        return min(r,l)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        for i in range(ceil(n/2)+1):\n",
    "            if words[(startIndex + i) % n] == target or  words[(startIndex - i)] == target:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        最短距离 = float('inf')\n",
    "        n = len(words)\n",
    "        for i, j in enumerate(words):\n",
    "            if j == target:\n",
    "                最短距离 = min(最短距离, abs(startIndex-i), n-abs(startIndex-i))\n",
    "        return -1 if 最短距离 == float(\"inf\") else 最短距离\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        \n",
    "        targetIndexs = []\n",
    "        for i in range(len(words)):\n",
    "            word = words[i]\n",
    "            if word == target:\n",
    "                targetIndexs.append(i)\n",
    "        \n",
    "        r = 999999\n",
    "        for targetIndex in targetIndexs:\n",
    "            if targetIndex == startIndex:\n",
    "                return 0\n",
    "            # zai zuobian\n",
    "            if targetIndex < startIndex:\n",
    "                # 向左\n",
    "                t1 = startIndex - targetIndex\n",
    "                # 向右\n",
    "                t2 = len(words) - startIndex + targetIndex\n",
    "                r = min(r, t1, t2)\n",
    "            else:\n",
    "                # zai youce\n",
    "                # 向右\n",
    "                t1 = targetIndex - startIndex\n",
    "                # 向左\n",
    "                t2 = startIndex + len(words) - targetIndex\n",
    "                r = min(r, t1, t2)\n",
    "            \n",
    "        return r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        length = len(words)\n",
    "        target_idx = -1\n",
    "        for i in range(startIndex, length + startIndex):\n",
    "            if words[i % length] == target:\n",
    "                new_diff = min((startIndex - i + length ) % length, (i - startIndex) % length)\n",
    "                if target_idx == -1 or new_diff < target_idx:\n",
    "                    target_idx = new_diff\n",
    "        return target_idx\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                d = abs(i - startIndex)\n",
    "                ans = min(ans, d, n - d)\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        for i in range(n // 2 + 1):\n",
    "            if words[(startIndex + i) % n] == target:\n",
    "                return i\n",
    "            if words[(startIndex - i) % n] == target:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        for i in range(ceil(n/2)+1):\n",
    "            if words[(startIndex + i) % n] == target or  words[(startIndex - i)] == target:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        index_list=[]\n",
    "        n=len(words)\n",
    "        ans1 = float('inf')\n",
    "        ans2 = float('inf')\n",
    "        for i in range(n):\n",
    "            if words[i] == target:\n",
    "                index_list.append(i)\n",
    "        if not index_list:\n",
    "            return -1\n",
    "        for index in index_list:\n",
    "            ans1 = min(ans1, (startIndex-index+n)%n)\n",
    "            ans2 = min(ans2, (index-startIndex+n)%n)\n",
    "        return min(ans1, ans2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        if startIndex == words.index(target):\n",
    "            return 0\n",
    "\n",
    "        tempwords = words * 2\n",
    "        if startIndex == 0:\n",
    "            temp = words.index(target)\n",
    "            return min([temp, len(words) - temp])\n",
    "\n",
    "        if words.index(target) < startIndex:\n",
    "            left = tempwords[:startIndex+1][::-1]\n",
    "            right = tempwords[startIndex:]\n",
    "            return min([left.index(target), right.index(target)])\n",
    "\n",
    "        if words.index(target) > startIndex:\n",
    "            left = words + words[:startIndex+1]\n",
    "            left = left[::-1]\n",
    "            right = words[startIndex:]\n",
    "            return min([left.index(target), right.index(target)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        if target == words[startIndex]:\n",
    "            return 0\n",
    "        i, n = 1, len(words)\n",
    "        while i < n:\n",
    "            a = (startIndex - i + n) % n\n",
    "            b = (startIndex + i) % n\n",
    "            if words[a] == target or words[b] == target:\n",
    "                return i\n",
    "            i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        \n",
    "        left_ans = (words[startIndex::-1]+words[:startIndex:-1]).index(target)\n",
    "        right_ans = (words[startIndex:]+words[0:startIndex]).index(target)\n",
    "        return min(left_ans, right_ans)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n=len(words)\n",
    "        ans=inf\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        for i in range(startIndex,startIndex+n):\n",
    "            if words[i%n]==target:\n",
    "                 ans=min(ans,min(i-startIndex,abs(startIndex-i%n),n-(i-startIndex)))\n",
    "        return ans\n",
    "                         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n=len(words)\n",
    "        r=n\n",
    "        for i in range(n):\n",
    "            if words[i]==target:\n",
    "                r=min(r,abs(i-startIndex),abs(i+n-startIndex),abs(startIndex+n-i))\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        # 1、向左和向右分别遍历\n",
    "        # 2、一次遍历\n",
    "        res = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if(w == target):\n",
    "                res = min(res, abs(startIndex - i), n - abs(startIndex - i))\n",
    "        return res if res < n else -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        res = -1\n",
    "        for i in range(startIndex, n + startIndex):\n",
    "            if words[i%n] == target:\n",
    "                res = i - startIndex\n",
    "                break\n",
    "        if res == -1:\n",
    "            return -1\n",
    "        for i in range(startIndex, startIndex - n, -1):\n",
    "            if words[i] == target:\n",
    "                res = min(res, startIndex - i)\n",
    "                break\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        else:\n",
    "            ans = len(words)\n",
    "            id_t = []\n",
    "            for i in range(0,len(words)):\n",
    "                if words[i] == target:\n",
    "                    id_t.append(i)\n",
    "            print(id_t)\n",
    "            for i in id_t:\n",
    "                ans = min(ans,abs(i - startIndex),len(words) - (abs(i - startIndex)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        min_distance = float('inf')\n",
    "        \n",
    "        # 向右遍历（向下一个元素方向）\n",
    "        distance = 0\n",
    "        index = startIndex\n",
    "        while True:\n",
    "            if words[index] == target:\n",
    "                min_distance = min(min_distance, distance)\n",
    "            \n",
    "            index = (index + 1) % n\n",
    "            distance += 1\n",
    "            \n",
    "            if index == startIndex:\n",
    "                break\n",
    "        \n",
    "        # 向左遍历（向前一个元素方向）\n",
    "        distance = 0\n",
    "        index = startIndex\n",
    "        while True:\n",
    "            if words[index] == target:\n",
    "                min_distance = min(min_distance, distance)\n",
    "            \n",
    "            index = (index - 1 + n) % n\n",
    "            distance += 1\n",
    "            \n",
    "            if index == startIndex:\n",
    "                break\n",
    "        \n",
    "        # 如果没有找到 target，返回 -1\n",
    "        return min_distance if min_distance != float('inf') else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def closetTarget(self, words: list[str], target: str, startIndex: int) -> int:\r\n",
    "        if(target not in words):\r\n",
    "            return -1\r\n",
    "        list1 = []\r\n",
    "        for i in range(len(words)):\r\n",
    "            if(words[i] == target):\r\n",
    "                list1.append(i)\r\n",
    "        m = 200\r\n",
    "        for i in range(len(list1)):\r\n",
    "            n = min(abs(startIndex-list1[i]),startIndex+len(words)-list1[i],len(words)-startIndex+list1[i])\r\n",
    "            if(n < m ):\r\n",
    "                m = n\r\n",
    "        return m\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n = len(words)\n",
    "        ans = set()\n",
    "        for i in range(n):\n",
    "            if words[i] == target:\n",
    "                ans.add(i)\n",
    "        if not ans:\n",
    "            return -1\n",
    "        res = n\n",
    "        for num in ans:\n",
    "            res = min(res, abs(n-num+startIndex), abs(num-startIndex), abs(n-startIndex+num))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        n = len(words)\n",
    "        ans = n + 1\n",
    "        for i in range(n):\n",
    "            if words[i] == target:\n",
    "                ans = min(ans, abs(i - startIndex), abs(n + i - startIndex), abs(i - n - startIndex))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        if target not in words:\n",
    "            return -1\n",
    "        else:\n",
    "            ans = len(words)\n",
    "            id_t = []\n",
    "            for i in range(0,len(words)):\n",
    "                if words[i] == target:\n",
    "                    id_t.append(i)\n",
    "            print(id_t)\n",
    "            for i in id_t:\n",
    "                ans = min(ans,abs(i - startIndex),len(words) - (abs(i - startIndex)))\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        ans = n = len(words)\n",
    "        for i, w in enumerate(words):\n",
    "            if w == target:\n",
    "                ans = min(ans, abs(i - startIndex), n - abs(i - startIndex))\n",
    "        return ans if ans < n else -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, w: List[str], t: str, b: int) -> int:\n",
    "        if t not in w:\n",
    "            return -1\n",
    "        n=len(w)\n",
    "        ans=inf\n",
    "        for i in range(n):\n",
    "            if w[(b+i)%n]==t:\n",
    "                ans=min(ans,i)\n",
    "                break\n",
    "        for i in range(n):\n",
    "            if w[(b-i+n)%n]==t:\n",
    "                ans=min(ans,i)\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        n=len(words)\n",
    "        ans=n\n",
    "        for index,value in enumerate(words):\n",
    "            if value==target:\n",
    "                ans=min(ans,abs(index-startIndex),n-abs(index-startIndex))\n",
    "        if ans <n:\n",
    "            return ans\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def closetTarget(self, words: List[str], target: str, startIndex: int) -> int:\n",
    "        # ans = []\n",
    "        # for i in len(words):\n",
    "        #     if words[i] == target:\n",
    "        #         ans.append(i)\n",
    "        # if len(ans) == 0:\n",
    "        #     return -1\n",
    "        for i in range(len(words)):\n",
    "            if words[(i + startIndex) % len(words)] == target or words[(startIndex - i + len(words)) % len(words)] == target:\n",
    "                return i\n",
    "        return -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
